package com.zhu.tool_test.redis;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhu.tool_test.dao.EnterpriseBasicMessageMapper;
import com.zhu.tool_test.entity.Device;
import com.zhu.tool_test.entity.EnterpriseBasicMessage;
import com.zhu.tool_test.util.EpMultipartData;
import core.MultipartData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: tool_test
 * @description:
 * @author: ggBall
 * @create: 2020-11-13 11:58
 **/
@Slf4j
@Component
public class RedisTest {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired
    EnterpriseBasicMessageMapper enterpriseBasicMessageMapper;

    /**
    * @Description 将查询的数据插入到redis
    * @Author  ggBall
    * @Date   2020/11/13 12:00
    * @Param
    * @Exception
    *
    */
    public void insertDeviceToRedis(){

        ObjectMapper mapper = new ObjectMapper();

        // 查询device数据
        Device device = new Device();
        List<Device> devices = device.selectAll();
        List<MultipartData> multipartDataList = EpMultipartData.trans2MultipartData(devices);
        // 扩大list容量 三层
        List<Object> list = multipartDataList.stream().map(dev -> {
//            List<MultipartData> dataList = EpMultipartData.trans2MultipartData(devices);
//            dataList.stream().map(data -> {
//                return data.include("duty", devices);
//            }).collect(Collectors.toList());
            try {
                String value = mapper.writeValueAsString(dev);
                //redisTemplate.opsForHash().put("device","123",value);
                redisTemplate.opsForList().leftPush("dev",value);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
        // 存入redis
        //redisTemplate.opsForList().leftPush("device", JSONObject.toJSONString(devices));
    }


    /**
     * @Description 将查询的数据插入到redis
     * @Author  ggBall
     * @Date   2020/11/13 12:00
     * @Param
     * @Exception
     *
     */
    public void insertEmpToRedis(){



        // 查询企业数据
        List<EnterpriseBasicMessage> enterpriseBasicMessages = enterpriseBasicMessageMapper.selectList(null);

        // 存入redis
        redisTemplate.opsForHash().put("ent","ent",enterpriseBasicMessages);    }

    public void insertHash(String key,String result){
        redisTemplate.opsForHash().put(key,key,result);
    }

    public void queryRedisValue(){
        //Object deviceList = redisTemplate.opsForList().leftPop("dev");
        //Object deviceHash = redisTemplate.opsForHash().get("device", "123");

        // 取出所有device的list
        List dev = redisTemplate.opsForList().range("ent", 0, -1);

        dev.stream().forEach(device -> System.out.println("device = " + device));

        //log.info("deviceList:{}",dev);
        // log.info("deviceList:{}",deviceList);
        log.info("dev:{}",dev);
        //log.info("deviceHash:{}",deviceHash);
    }

    public Object redisIsHave(String key){
        Object o1 = redisTemplate.opsForHash().get(key, key);
        List<EnterpriseBasicMessage> o = (List<EnterpriseBasicMessage>)redisTemplate.opsForHash().get(key, key);
        return o;
    }
}
